package com.bjpowernode.web.settings;

import com.bjpowernode.web.interceptor.TokenInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

/**
 * @description:
 * @author: panDeng_Zhu
 * @time: 2022/11/7 23:01
 */
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {

    @Value("${jwt-secretKey}")
    private String userTokenKey;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册拦截器
        TokenInterceptor tokenInterceptor =new TokenInterceptor(stringRedisTemplate, userTokenKey);

        //地址会修改
        String [] addPath ={"/v1/user/realname",
                "/v1/user/info",
                "/v1/invest/product",
                "/v1/invest/info",
                "/v1/recharge/info",
                "/v1/income/info",
                "/v1/user/logout"
        };
        registry.addInterceptor(tokenInterceptor).addPathPatterns(addPath);
    }

    /*支持跨域*/
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE", "OPTIONS")
                .allowCredentials(true)
                .maxAge(3600)
                .allowedHeaders("*");
    }
}
